﻿using Core.Framework.Register;
using Microsoft.Extensions.Options;
using Nacos.AspNetCore.V2;
using Nacos.V2;
using Nacos.V2.Naming.Dtos;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static System.Net.WebRequestMethods;

namespace Nacos.Framework
{
    public class NacosRegister : IRegister
    {
        private readonly INacosNamingService _svc;
        private readonly NacosAspNetOptions _registerOptions;

        public NacosRegister(INacosNamingService svc, IOptionsMonitor<NacosAspNetOptions> registerOptions)
        {
            _svc = svc;
            _registerOptions = registerOptions.CurrentValue;
        }

        public async Task RegistAsync()
        {
            await _svc.RegisterInstance(_registerOptions.ServiceName, _registerOptions.GroupName, _registerOptions.Ip, _registerOptions.Port); 
        }

        public async Task<Instance> GetInstance(string serviceName)
        {
            // 这里需要知道被调用方的服务名
            return await _svc.SelectOneHealthyInstance(serviceName, "DEFAULT_GROUP");
        }
    }
}
